package com.test.webview;


import com.test.webview.HomeProvider;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebViewFragment;
import android.util.Log;
public class webview extends Activity {
	private WebView mWebView;
	final Activity mActivity = this;
    private final WebViewClient mWebViewClient = new WebViewClient() {
    	@Override    
    	public boolean shouldOverrideUrlLoading(WebView view, String url) {      
    		if (!url.startsWith("content://")) {
    		    Uri uri = Uri.parse(url);
    		    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    		    startActivity(intent);
    		}else if (url.startsWith("content://")){
    			mWebView.loadUrl("content://com.test.webview.home/");  
    		}
    	  return true;    
    		}
    	 @Override
         public WebResourceResponse shouldInterceptRequest(WebView view,
                 String url) {
             WebResourceResponse res = HomeProvider.shouldInterceptRequest(
                     mActivity, url);
             return res;
         }
    	 @Override
    	 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
    		 int e = event.getKeyCode ();
    		 if ((event.getAction () == event.ACTION_DOWN)&&(e>= event.KEYCODE_DPAD_UP)&&(e<= event.KEYCODE_DPAD_RIGHT))
    		 {
    		     view.loadUrl("javascript:switchFocus('" + e + "')");
    	         return true;
    		 }else{
    			 return false;
    		 }
    	    }
    };
    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebViewFragment fragment = (WebViewFragment)getFragmentManager().findFragmentById(R.id.fragment);
        mWebView = fragment.getWebView(); 
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("content://com.test.webview.home/");
        mWebView.setWebViewClient(mWebViewClient);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {   
    	if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    	}
    
}